unit ManageClientUnit;

interface

uses
  Classes,
  SysUtils,
  IdTCPClient,
  IdTCPConnection,
  ConstUnit;

type
  TManageClient = class
  private
    FTcpCli: TIdTCPClient;
  public
    constructor Create(AHost: string; APort: Integer = ServManagePort);
    destructor Destroy; override;

    function DoLogin(APassword: string; var AMessage: string): Boolean;
    function DoLogout: Boolean;
    
    function DoReadData(AData: string): string;
    function DoWriteData(AData: string; var AMessage: string): Boolean;
    function DoReadSysConfig(AData: string): string;
    function DoWriteSysConfig( AData: string; var AMessage: string): Boolean;
    function DoGetBinaryFromDB( AData: string; AStream: TStream): Boolean;
    function DoGetBinaryFromFile( AData: string; AStream: TStream): Boolean;
  end;

implementation

{ TManageClient }

constructor TManageClient.Create(AHost: string; APort: Integer);
begin
  FTcpCli := TIdTCPClient.Create( nil );
  FTcpCli.Host := AHost;
  FTcpCli.Port := APort;
end;

destructor TManageClient.Destroy;
begin
  if FTcpCli.Connected then
    FTcpCli.Disconnect;
  FTcpCli.Free;
  inherited;
end;

function TManageClient.DoGetBinaryFromDB(AData: string;
  AStream: TStream): Boolean;
begin
  Result := False;
  if not Assigned( AStream ) then
    Exit;
    
  FTcpCli.WriteInteger( MgrGetBinaryFromDB );
  FTcpCli.ReadStream( AStream );
end;

function TManageClient.DoGetBinaryFromFile(AData: string;
  AStream: TStream): Boolean;
begin

end;

function TManageClient.DoLogin(APassword: string;
  var AMessage: string): Boolean;
var
  Data: string;
begin
  Result := False;
  if not FTcpCli.Connected then
  try
    FTcpCli.Connect;
  except
    on E: Exception do
    begin
//      if E is EIdTCPConnectionError then
        AMessage := Format('%s, %s', [E.ClassName, E.Message]);
      Exit;
    end;
  end;

  FTcpCli.WriteInteger( MgrLogin );
  FTcpCli.WriteLn( APassword );

  Data := FTcpCli.ReadLn;
  if Data = 'OK' then
    Result := True
  else
    AMessage := Data;
  
end;

function TManageClient.DoLogout: Boolean;
begin
  FTcpCli.WriteInteger( MgrLogout );
  FTcpCli.Disconnect;
end;

function TManageClient.DoReadData(AData: string): string;
begin
  FTcpCli.WriteLn( AData );
end;

function TManageClient.DoReadSysConfig(AData: string): string;
begin
  FTcpCli.WriteInteger( MgrReadSysConfig );
  FTcpCli.WriteLn( AData );
  Result := FTcpCli.ReadLn;
end;

function TManageClient.DoWriteData(AData: string;
  var AMessage: string): Boolean;
begin
  FTcpCli.WriteInteger( MgrWriteData );
  FTcpCli.WriteLn( AData );
  AMessage := FTcpCli.ReadLn;
  Result := ( AMessage = MgrOK );
end;

function TManageClient.DoWriteSysConfig(AData: string;
  var AMessage: string): Boolean;
begin
  FTcpCli.WriteInteger( MgrWriteSysConfig );
  FTcpCli.WriteLn( AData );
  AMessage := FTcpCli.Readln;
  Result := ( AMessage = MgrOK );
end;

end.
